package network;

import graphics.Shape;
import graphics.ShapeFactory;
import data.ChatMessages;
import data.ShapesData;
import data.UserList;

public class MessageProcessor {
	private static MessageProcessor instance = null;
	public static MessageProcessor getInstance()
	{
		if(instance == null)
		{
			instance = new MessageProcessor();
		}
		return instance;
	}
private MessageProcessor() {
	
}

public void process(Message m)
{
	new Processor(m).start();
}
}
class Processor extends Thread
{
	Message msg = null;
	public Processor(Message m) {
		msg = m;
	}
	
	@Override
	public void run() {
		switch (msg.getType()) {
		case MessageType.CHECK_ALIVE:
			break;
		case MessageType.USERNAME_SYNC:
			String list = msg.getMessageBody();
			UserList.getInstance().clear();
			String[] str = list.split(",");
			for(int i=0;i<str.length;i++)
			{
				if(str[i] !=null && !"".equals(str[i]))UserList.getInstance().addClient(str[i]);
			}
//			UserList.getInstance().print();
			break;
		case MessageType.DRAW_SYNC:
			String[] shapes = msg.getMessageBody().split("$");
			for(String s:shapes)
			{
				if(s !=null && !"".equals(s))
				{
					Shape ss = ShapeFactory.createShapeByString(s);
					ss.fromString(s);
					ShapesData.getInstance().addShape(ss);
				}
			}
			break;
		case MessageType.DRAW:
			String data = msg.getMessageBody();
			Shape s = ShapeFactory.createShapeByString(data);
			s.fromString(data);
			ShapesData.getInstance().addShape(s);
			break;
		case MessageType.CHAT:
			String data1 = msg.getMessageBody();
			String[] d = data1.split(":");
			ChatMessages.getInstance().addMessage(d[0], d[1]);
			break;
		default:
			break;
		}
	}
}